package service

import (
	"context"
	"errors"
	"golang.org/x/sync/errgroup"
	"week4/api/user"
	"week4/app/user/internal/biz"
)

var (
	ErrUserServiceInternal = errors.New("user Service internal")
	ErrUserNotFound        = errors.New("user not found")
)

func (u *UserService) UserMy(ctx context.Context, r *user.UserRequest) (*user.UserMyReply, error) {
	// 领域业务拼接
	userBase := biz.UserBase{}
	vipInfo := biz.VipInfo{}

	wg, sctp := errgroup.WithContext(ctx)

	wg.Go(func() error {
		userB, err := u.uc.GetUserBase(sctp, r.UserID)
		if err != nil {
			return err
		}
		userBase = userB
		return nil
	})

	wg.Go(func() error {
		vipI, err := u.vc.GetVipInfo(sctp, r.UserID)
		if err != nil {
			return err
		}
		vipInfo = vipI
		return nil
	})

	if err := wg.Wait(); err != nil {
		if errors.Is(err, biz.ErrUserNotFound) {
			return &user.UserMyReply{}, err
		} else {
			return &user.UserMyReply{}, ErrUserServiceInternal
		}
	} else {
		return &user.UserMyReply{Name: userBase.Name, VipGrade: vipInfo.VipGrade}, nil
	}
}
